package com.itita.weibo.model;

import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.User;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itita.weibo.manager.MainService;
import com.itita.weibo.ui.R;



//测试Adapter
public class CommentToMeAdapter extends BaseAdapter {
	
	public List<Comment> comments;
	
	private Context context;

	private CommentHolder commHolder = new CommentHolder();
	

	public CommentToMeAdapter(Context con, List<Comment> list) {
		this.comments = list;
		this.context = con;
	}

	public int getCount() {
		return comments.size() + 2;
	}

	public Object getItem(int position) {
		if (position == 0){
			return null;
		}else if (position == this.getCount() - 1){
			return null;
		}
		// 返回这条微薄对象
		else{
			return comments.get(position - 1);
		}
	}

	public long getItemId(int position) {
		if (position == 0){// 刷新
			return 0;
		} else if (position > 0 && position < this.getCount() - 1){// 返回微博的ID
			return comments.get(position - 1).getId();
		} else { // 选中了更多
			return -1;
		}
	}

	/**
	 * 动态给StatusAdapter添加数据
	 * @param adddata
	 */
	public void addMoreData(List<Comment> adddata) {
		this.comments.addAll(adddata);
		this.notifyDataSetChanged();
	}

	
	

	public View getView(int position, View convertView, ViewGroup parent) {
		if (position == 0)// 刷新
		{
			View commItem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) commItem.findViewById(R.id.tvItemContent);
			tv.setText("刷新");
			return commItem;
		} else if (position == this.getCount() - 1)// 更多
		{
			View commItem = LayoutInflater.from(context).inflate(
					R.layout.moreitemsview, null);
			TextView tv = (TextView) commItem.findViewById(R.id.tvItemContent);
			tv.setText("更多");
			return commItem;
		} else {
			View commItem = null;

			if (convertView != null && convertView.findViewById(R.id.tvItemName) != null) {
				Log.d("getview", "doGetView-------get TextView-----------"
						+ position);
				commItem = convertView;
			} else {
				Log.d("getview", "doGetView-------new TextView-----------"
						+ position);
				// 把xml布局文件变成View对象
				commItem = LayoutInflater.from(context).inflate(R.layout.itemview_cmm_to_me, null);
			}
			
			this.initWeiboHoler(commItem);
			
			Comment comment = this.comments.get(position-1);
			this.commHolder.tvICommentUsername.setText(comment.getUser().getName());
			this.commHolder.tvCommContent.setText(comment.getText());
			//--------------------->
			this.commHolder.tvCommSource.setText(
					new StringBuffer().append("恢复我的微博:\"")
					.append(comment.getStatus().getText())
					.append("\""));
			this.commHolder.tvItemCommDate.setText(comment.getCreatedAt().toLocaleString());
			User user = comment.getUser();
			
			if(MainService.allIcon.get(user.getId()) != null){
				this.commHolder.ivItemCommPortrait.setImageBitmap(MainService.allIcon.get(user.getId()));
			}
			try {
				////System.out.println(comment.getStatus().getText());
			} catch (Exception e) {
				 e.printStackTrace();
				 System.err.println("ERROR:comment.getStatus().getText()" );
			}
			////System.out.println("-------对了--------------");
			return commItem;
		}
	}

	private void initWeiboHoler(View commItem) {
		this.commHolder.ivItemCommPortrait = (ImageView) commItem.findViewById(R.id.ivItemCommPortrait);
		this.commHolder.tvICommentUsername = (TextView) commItem.findViewById(R.id.tvItemCommName);
		this.commHolder.tvCommContent = (TextView) commItem.findViewById(R.id.tvItemCommContent);
		this.commHolder.tvCommSource = (TextView) commItem.findViewById(R.id.tvItemSourceContent);
	    this.commHolder.tvItemCommDate = (TextView) commItem.findViewById(R.id.tvItemCommDate);
	}
}